package com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.repository.HddcWyGeogeomorphysvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.repository.HddcWyGeogeomorphysvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.repository.entity.HddcWyGeogeomorphysvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.service.HddcWyGeogeomorphysvypointService;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.viewobjects.HddcWyGeogeomorphysvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeoGeomorphySvyPoint.viewobjects.HddcWyGeogeomorphysvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyGeogeomorphysvypointServiceImpl implements HddcWyGeogeomorphysvypointService {

	@Autowired
    private HddcWyGeogeomorphysvypointRepository hddcWyGeogeomorphysvypointRepository;
    @Autowired
    private HddcWyGeogeomorphysvypointNativeRepository hddcWyGeogeomorphysvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyGeogeomorphysvypoints(HddcWyGeogeomorphysvypointQueryParams queryParams ,
                                                       int curPage, int pageSize) {
        Page<HddcWyGeogeomorphysvypointEntity> hddcWyGeogeomorphysvypointPage =
                this.hddcWyGeogeomorphysvypointNativeRepository.queryHddcWyGeogeomorphysvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeogeomorphysvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeogeomorphysvypointEntity getHddcWyGeogeomorphysvypoint(String id) {
        HddcWyGeogeomorphysvypointEntity hddcWyGeogeomorphysvypoint = this.hddcWyGeogeomorphysvypointRepository.findById(id).orElse(null);
         return hddcWyGeogeomorphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeogeomorphysvypointEntity saveHddcWyGeogeomorphysvypoint(HddcWyGeogeomorphysvypointEntity hddcWyGeogeomorphysvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeogeomorphysvypoint.setId(uuid);
        hddcWyGeogeomorphysvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeogeomorphysvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeogeomorphysvypoint.setIsValid("1");
        this.hddcWyGeogeomorphysvypointRepository.save(hddcWyGeogeomorphysvypoint);
        return hddcWyGeogeomorphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeogeomorphysvypointEntity updateHddcWyGeogeomorphysvypoint(HddcWyGeogeomorphysvypointEntity hddcWyGeogeomorphysvypoint) {
        hddcWyGeogeomorphysvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeogeomorphysvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeogeomorphysvypointRepository.save(hddcWyGeogeomorphysvypoint);
        return hddcWyGeogeomorphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeogeomorphysvypoints(List<String> ids) {
        List<HddcWyGeogeomorphysvypointEntity> hddcWyGeogeomorphysvypointList = this.hddcWyGeogeomorphysvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeogeomorphysvypointList) && hddcWyGeogeomorphysvypointList.size() > 0) {
            for(HddcWyGeogeomorphysvypointEntity hddcWyGeogeomorphysvypoint : hddcWyGeogeomorphysvypointList) {
//                this.hddcWyGeogeomorphysvypointRepository.delete(hddcWyGeogeomorphysvypoint);
                hddcWyGeogeomorphysvypoint.setIsValid("0");
                hddcWyGeogeomorphysvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeogeomorphysvypoint.setUpdateTime(new Date());
                this.hddcWyGeogeomorphysvypointRepository.save(hddcWyGeogeomorphysvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeogeomorphysvypoint(HddcWyGeogeomorphysvypointQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeogeomorphysvypointNativeRepository.queryHddcWyGeogeomorphysvypoint(queryParams);
        return bigInteger;
    }

}
